package cn.hs.controller;

import cn.hs.entity.Publicity;
import cn.hs.entity.Question;
import cn.hs.service.QuestionService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName QuestionController
 * @Description TODO
 * @Author wsh
 * @Date 2024年10月24日 17:06
 * @Version 1.0
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @PostMapping("/insert")
    public Result insert(@RequestBody Question question){
        int add = questionService.insert(question);
        if (add == 1) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }
    @PostMapping("/update")
    public Result update(@RequestBody Question question){
        int edit = questionService.update(question);
        if(edit==1){
            return  Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
    @GetMapping("/find")
    public Result find(@RequestBody(required = false) Question question){
        List<Question> list=questionService.find(question);
        Result result=Result.success(list);
        return result;
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
//        System.out.println("id = " + id);
//        long l = Long.parseLong(id);
        int remove = questionService.delete(id);
        if(remove==1){
            return  Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(value = "pageNum", required = false,
                    defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false,
                    defaultValue = "5") Integer pageSize,
            Question question
    ) {
        PageInfo<Publicity> pageInfo = questionService.findAll(pageNum, pageSize,question);
        return Result.success(pageInfo);
    }
}
